package ${serviceImplPackage};

import ${servicePackage}.${domainObjectName}Service;
import ${modelPackage}.${domainObjectName}DTO;
import ${entityPackage}.${domainObjectName};
import ${managerPackage}.${domainObjectName}Manager;
import ${mapstructPackage}.${domainObjectName}Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${tableComment!} 服务实现类
 * @author ${author!}
 * @date ${date?string("yyyy-MM-dd")}
 */
@Service
public class ${domainObjectName}ServiceImpl implements ${domainObjectName}Service {

    @Autowired
    private ${domainObjectName}Manager ${domainObjectName?uncap_first}Manager;

    @Autowired
    private ${domainObjectName}Mapper ${domainObjectName?uncap_first}Mapper;

    @Override
    public int insert(${domainObjectName}DTO ${domainObjectName?uncap_first}DTO) {
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Mapper.toEntity(${domainObjectName?uncap_first}DTO);
        return ${domainObjectName?uncap_first}Manager.insert(${domainObjectName?uncap_first});
    }

    @Override
    public int update(${domainObjectName}DTO ${domainObjectName?uncap_first}DTO) {
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Mapper.toEntity(${domainObjectName?uncap_first}DTO);
        return ${domainObjectName?uncap_first}Manager.update(${domainObjectName?uncap_first});
    }

    @Override
    public int deleteById(${primaryKeyJavaType} id) {
        return ${domainObjectName?uncap_first}Manager.deleteById(id);
    }

    @Override
    public ${domainObjectName}DTO selectById(${primaryKeyJavaType} id) {
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Manager.selectById(id);
        return ${domainObjectName?uncap_first}Mapper.toDTO(${domainObjectName?uncap_first});
    }

    @Override
    public List<${domainObjectName}DTO> selectAll() {
        List<${domainObjectName}> list = ${domainObjectName?uncap_first}Manager.selectAll();
        return list.stream()
                .map(${domainObjectName?uncap_first}Mapper::toDTO)
                .collect(Collectors.toList());
    }
}

<#-- 查找主键类型 -->
<#assign primaryKeyJavaType = "Long">
<#list columns as column>
    <#if column.primaryKey>
        <#assign primaryKeyJavaType = column.javaType>
    </#if>
</#list>
